package service

import (
	"context"
	"fmt"
	"gitee.com/cristiane/micro-mall-search-order-consumer/model/args"
	"gitee.com/cristiane/micro-mall-search-order-consumer/repository"
	"gitee.com/kelvins-io/common/errcode"
	"gitee.com/kelvins-io/common/json"
	"gitee.com/kelvins-io/kelvins"
)

func NoticeConsume(ctx context.Context, body string) error {
	var err error
	var msg args.CommonBusinessMsg
	err = json.Unmarshal(body, &msg)
	if err != nil {
		return err
	}
	if msg.Type != args.TradeOrderInfoSearchNoticeType {
		return nil
	}
	var orderInfo args.SearchTradeOrderEntry
	err = json.Unmarshal(msg.Content, &orderInfo)
	if err != nil {
		return err
	}
	retCode := repository.SearchStore(ctx, args.ESIndexTradeOrder, orderInfo.OrderCode, msg.Content)
	if retCode == errcode.SUCCESS {
		return nil
	}
	err = fmt.Errorf(errcode.GetErrMsg(retCode))
	kelvins.BusinessLogger.Errorf(ctx, "orderInfo SearchStore err: %v, content: %v", errcode.GetErrMsg(retCode), msg)

	return err
}

func NoticeConsumeErr(ctx context.Context, err, body string) error {
	return nil
}
